<?php

/**
 * 图文列表logic
 */

namespace Member\Logic;

class NewsmsgLogic extends BaseLogic {
    
	/**
	 * 获取来自表单的onenews信息
	 */
	public function getOnenews(){
		/**
		array (size=9)
		  'Title' => string 'ss' (length=2)
		  'ImgPath' => string '/wechat/www/Public/Upload/2013/1222/52b6ed5b5152a.jpg' (length=53)
		  'BriefDescription' => string 'ssss' (length=4)
		  'Url' => string 'app_estate/' (length=11)  //列表选择
		  'oUrl' => string '' (length=0)  //这个是修改的时候显示的之前的url
		  'submit_button' => string '提交保存' (length=12)
		  'MId' => string '0' (length=1)
		  'MaterialType' => string '0' (length=1)
		  'do_action' => string 'wechat.material' (length=15)
		 */
	    $data = array(
    		'Title'=>I('post.Title'),
	    	'BriefDescription'=>I('post.BriefDescription'),
	    	'ImgPath'=>I('post.ImgPath'),
	    	'Url'=>I('post.Url'),
	    	'oUrl'=>I('post.oUrl'),
	    	'MId'=>I('post.MId','','intval'),
	    );
	    return $data;
	}
	
	/**
	 * 保存单挑图文信息
	 */
	public function saveOnenews($data = array()){
		
		if(empty($data)){
			//获取传过来的信息
			$data = $this->getOnenews();
		}
		
	    //判断链接是否是正确的
	    if(empty($data['Url']) || !isset($data['Url'])){
	        $data['url'] = isset($data['oUrl']) ? $data['oUrl'] : '';
	    }
	    //删除该信息
	    unset($data['oUrl']);
	    
	    //判断当前是在更新还是新添加信息
	    $id = $data['MId'];
	    unset($data['MId']);
	    if($id > 0){
	    	
	    	$update = array(
	    		'title'=>$data['Title'],
	    	    'configs' => serializeMysql($data),
	    	);
	    	
	        return $this->where(array('id'=>$id))->save($update);
	        
	    }else {
	    	$insert = array(
	    			'uid'=>UID,
	    			'title'=>$data['Title'],
	    			'relation'=>'',
	    			'msgtype'=>'onenews',
	    			'configs' => serializeMysql($data),
	    			'createtime'=>time(),
	    	);
	    	return $this->add($insert);
// 	    	 echo $this->getLastSql();EXIT;
	    }
	    
	}
	
	
	
	/**
	 * 获取多图文消息
	 */
	public function getNews($id=0){
	    /**
	     * array (size=9)
  'Title' => 
    array (size=3)
      0 => string '头条' (length=6)
      1 => string '第二条' (length=9)
      2 => string '第三条' (length=9)
  'Url' => 
    array (size=3)
      0 => string 'web/column/4434/' (length=16)
      1 => string 'web/column/4434/' (length=16)
      2 => string 'web/column/4435/' (length=16)
  'ImgPath' => 
    array (size=3)
      0 => string '/wechat/www/Public/Upload/2013/1223/52b7d3eca1f21.jpg' (length=53)
      1 => string '/wechat/www/Public/Upload/2013/1223/52b7d3f69e03b.jpg' (length=53)
      2 => string '/wechat/www/Public/Upload/2013/1223/52b7d40101bc8.jpg' (length=53)
  'inputTitle' => string '第三条' (length=9)
  'inputUrl' => string 'web/column/4435/' (length=16)
  'submit_button' => string '提交保存' (length=12)
  'MId' => string '0' (length=1)
  'MaterialType' => string '1' (length=1)
  'do_action' => string 'wechat.material' (length=15)
	     */
	}
	
	/**
	 * 保存多图文消息
	 * @param unknown $id
	 * @return Ambigous <string, mixed>
	 */
	public function saveNews(){
	    /**
	     *
	     array (size=8)
  'Title' => 
    array (size=2)
      0 => string 'sss' (length=3)
      1 => string 'sssss' (length=5)
  'Url' => 
    array (size=2)
      0 => string 'app_estate/' (length=11)
      1 => string 'web/column/4434/' (length=16)
  'ImgPath' => 
    array (size=2)
      0 => string '/wechat/www/Public/Upload/2014/0209/52f744fda1f8d.jpg' (length=53)
      1 => string '/wechat/www/Public/Upload/2014/0209/52f74507049f1.jpg' (length=53)
  'inputTitle' => string 'sssss' (length=5)
  'inputUrl' => string 'web/column/4434/' (length=16)
  'submit_button' => string '提交保存' (length=12)
  'MId' => string '0' (length=1)
  'msgtype' => string 'news' (length=4)
	     */
		
		$Title = I('post.Title');
		$Url = I('post.Url');
		$ImgPath = I('post.ImgPath');
		//数据信息
		$data = array();
		foreach ($Title as $k=>$v){
			//如果其中那个少了一个
		    if(!(isset($Url[$k]) && isset($ImgPath[$k]))){
		        continue;
		    }
		    
		    $data['configs'][] = array(
		        'Title'=>$v,
		    	'BriefDescription'=>'',
		    	'ImgPath'=>$ImgPath[$k],
		    	'Url'=>$Url[$k],
		    );
		}
		//如果里面没有数据
		if(empty($data)){
		    return false;
		}
		
		//设置标题
		$data['title'] = $data['configs'][0]['Title'];
		
		$data['configs'] = serializeMysql($data['configs']);
		
		//消息类型
		$data['msgtype'] = 'news';
		//用户
		$data['uid'] = UID;
		
		//判断是更新还是添加
		$id = I('post.id',0,'intval');
		if($id > 0){
			$news = $this->detail($id);
			if(empty($news)){
			    $id = 0;
			}
		}
// 		dump($_POST);dump($id);exit;
		//如果消息存在
		if($id > 0){
			$map = array('id'=>$id,'uid'=>UID);
		    $boolean = $this->where($map)->save($data);
		}else{
		    //添加信息
		    $data['createtime'] = time();
		    $boolean = $this->add($data);
		}
		
		if($boolean !== false){
		    return true;
		}else{
		    return false;
		}
		
	}
	
	public function getNewsRow($id){
		$map = array(
				'id'	=> $id,
		);
		$data = $this->where($map)->find();
		$configs = serializeMysql($data['configs'],1);
// 		$data['Title']	= $configs['Title'];
// 		$data['Title']	= $configs['Title'];
// 		$data['Title']	= $configs['Title'];
// 		$data['Title']	= $configs['Title'];
		$data['configs']	= $configs;
		return $data;
	}
	
	/**
	 * (non-PHPdoc)
	 * @see \Member\Logic\BaseLogic::update()
	 */
	public function update($id = 0){
	    
	}
	
	/**
	 * (non-PHPdoc)
	 * @see \Member\Logic\BaseLogic::autoSave()
	 */
	public function autoSave($id = 0){
		 
	}
	
	/**
	 * 获取当前用户的全部的图文消息
	 */
	public function allLists(){
		//查询条件
		$map = array(
			'uid'=>UID,
			//'relation'=>'',
		);
		return $this->where($map)->field('id,title,msgtype')->select();
	}
}